package com.scitc.pleasurevideo.data;

import android.content.Context;

import com.google.gson.Gson;
import com.scitc.pleasurevideo.base.ETNoBackFragmentActivity;
import com.scitc.pleasurevideo.helper.ThreadHelper;
import com.scitc.pleasurevideo.pojo.CachingTvVo;
import com.scitc.pleasurevideo.util.ConstUtil;
import com.scitc.pleasurevideo.util.IdGeneratorUtil;
import com.scitc.pleasurevideo.x5webview.MyApplication;

import java.util.ArrayList;
import java.util.List;

import static com.scitc.pleasurevideo.data.ETMan.getPref;
import static com.scitc.pleasurevideo.data.ETMan.savePref;

public class HistoryTvDataMan {

    private static HistoryTvDataMan historyTvDataMan;

    private HistoryTvDataMan() {
        super();
    }

    private List<String> historyTvArray = new ArrayList<>();
    private List<CachingTvVo> cachingTvVos = new ArrayList<>();
    private Context mcontext;

    public synchronized static HistoryTvDataMan getHistoryTvDataMan() {
        if (historyTvDataMan == null) {
            historyTvDataMan = new HistoryTvDataMan();
        }
        historyTvDataMan.setContext();
        historyTvDataMan.seleteString();
        return historyTvDataMan;
    }

    private void setContext() {
        this.mcontext = ETNoBackFragmentActivity.mContext;
    }

    public boolean checkHistoryItemNum() {
        return historyTvArray != null && historyTvArray.size() >= 1;
    }

    public void saveHistoryTvDataMan(final CachingTvVo cachingTvVo) {
        ThreadHelper.run(new ThreadHelper.BackThreadListener() {
            @Override
            public void background() {
                saveJudge(cachingTvVo,ETMan.getPref(mcontext, ConstUtil.IS_HISTORY_DIFFERENT_SOURCE, ConstUtil.HISTORY_TYPE_DEFAULT));
            }
        });
    }

    public void saveHistoryTvDataMan(final CachingTvVo cachingTvVo,int type) {
        ThreadHelper.run(new ThreadHelper.BackThreadListener() {
            @Override
            public void background() {
                if(type>=0){
                    saveJudge(cachingTvVo,type);
                }else {
                    saveHistoryTvDataMan(cachingTvVo);
                }
            }
        });
    }

    public void deleteHistoryTvDataMan(final String code) {

        ThreadHelper.run(new ThreadHelper.BackThreadListener() {
            @Override
            public void background() {
                deleteItem(code);
                deleteString(code);
            }
        });
    }

    public void clearHistoryTvDataMen() {
        ThreadHelper.run(new ThreadHelper.BackThreadListener() {
            @Override
            public void background() {
                clearItems();
                clearString();
            }
        });
    }

    public List<CachingTvVo> seleteHistoryTvDataMan() {
        return seleteItems();
    }

    /**
     * 存储
     * @param cachingTvVo 存储数据
     * @param type 存储方式
     *             type=0:进行第一种存储--共存
     *             type=1:进行第二种存储--替换
     */
    private void saveJudge(CachingTvVo cachingTvVo,int type){
        String id;
        if(type==0){
            id=judgeIsAddOrIsUpdateOne(cachingTvVo);
        }else {
            id=judgeIsAddOrIsUpdateSecond(cachingTvVo);
        }
        if(id==null||id.length()<1){
            //增加
            String code = IdGeneratorUtil.getIdString("historyTv_");
            cachingTvVo.setId(code);
            saveString(code);
        }else {
            //修改
            cachingTvVo.setId(id);
        }
        saveItem(cachingTvVo);
    }


    /**
     * 第一种判断
     * 判断该增加还是更新
     * 主要是判断是不是同一个视频，同一个来源，只是播放集数和总集数不同
     * @param cachingTvVo
     * @return id
     * 当id==null,增加
     * 当id.lenght>0,修改
     */
    private String judgeIsAddOrIsUpdateOne(CachingTvVo cachingTvVo){
        List<CachingTvVo> items = seleteHistoryTvDataMan();
        String id="";
        for (int i = 0; i < items.size(); i++) {
            if (items.get(i).getName().equals(cachingTvVo.getName()) &&
                    items.get(i).getUrl().equals(cachingTvVo.getUrl()) &&
                    items.get(i).getHtmlTitle().equals(cachingTvVo.getHtmlTitle()) &&
                    items.get(i).getImgUrl().equals(cachingTvVo.getImgUrl())) {
                id=items.get(i).getId();
                break;
            }
        }
        return id;
    }

    /**
     * 第二种判断
     * 判断该增加还是更新
     * 主要是判断是不是同一个视频，同一个来源，只是播放集数和总集数不同
     * @param cachingTvVo
     * @return id
     * 当id==null,增加
     * 当id.lenght>0,修改
     */
    private String judgeIsAddOrIsUpdateSecond(CachingTvVo cachingTvVo){
        List<CachingTvVo> items = seleteHistoryTvDataMan();
        String id="";
        for (int i = 0; i < items.size(); i++) {
            if (items.get(i).getName().equals(cachingTvVo.getName())) {
                id=items.get(i).getId();
                break;
            }
        }
        return id;
    }



    private void saveString(String code) {
        String json = getPref(mcontext, "config_caching", "[]");
        List<String> keywords = ETMan.getMananger().getGson().fromJson(json, List.class);
        keywords.add(code);
        json = ETMan.getMananger().getGson().toJson(keywords);
        savePref(mcontext, "config_caching", json);
    }

    private void deleteString(String code) {
        String json = getPref(mcontext, "config_caching", "[]");
        List<String> keywords = ETMan.getMananger().getGson().fromJson(json, List.class);
        keywords.remove(code);
        json = ETMan.getMananger().getGson().toJson(keywords);
        savePref(mcontext, "config_caching", json);
    }

    private void clearString() {
        savePref(mcontext, "config_caching", "[]");
    }

    private void seleteString() {
        String json = getPref(mcontext, "config_caching", "[]");
        historyTvArray = ETMan.getMananger().getGson().fromJson(json, List.class);
    }

    private void saveItem(CachingTvVo item) {
        String json = ETMan.getMananger().getGson().toJson(item);
        savePref(mcontext, "config_caching_" + item.getId(), json);
    }

    private void deleteItem(String code) {
        savePref(mcontext, "config_caching_" + code, "[]");
    }

    private void clearItems() {
        for (int i = 0; i < historyTvArray.size(); i++) {
            savePref(mcontext, "config_caching_" + historyTvArray.get(i), "[]");
        }
    }

    private List<CachingTvVo> seleteItems() {
        List<CachingTvVo> items = new ArrayList<>();

        for (int i = 0; i < historyTvArray.size(); i++) {

            String json = getPref(mcontext, "config_caching_" + historyTvArray.get(i), "[]");
            CachingTvVo news = new Gson().fromJson(json, CachingTvVo.class);
            items.add(news);

        }

        return items;
    }

}
